/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/component/OutputImpl.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: OutputImpl.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (Mon, 02 Jul 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.component.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import com.elancom.framework.component.Error;
import com.elancom.framework.component.Message;
import com.elancom.framework.component.Output;

/**
 * @author elancom
 * @date Apr 9, 2012
 */
@SuppressWarnings("unchecked")
public class OutputImpl implements Output {
	private static final long serialVersionUID = 2372815616674381922L;

	private Map<String, Object> parameter = new ConcurrentHashMap<String, Object>();

	private List<Message> messageList = new ArrayList<>();

	private List<Error> errorList = new ArrayList<>();

	OutputImpl() {
	}

	@Override
	public <T> T getParameter(String key) {
		return (T) parameter.get(key);
	}

	@Override
	public Output removeParameter(String key) {
		parameter.remove(key);
		return this;
	}

	@Override
	public Output addError(String key, String content) {
		Error error = new Error(key, content);
		errorList.add(error);

		return this;
	}

	@Override
	public Output addError(String key, String msg, int errorType) {
		Error error = new Error(key, msg);
		error.setErrorType(errorType);
		errorList.add(error);

		return this;
	}

	@Override
	public Output addError(Map<String, String> errorMap) {
		for (Entry<String, String> entry : errorMap.entrySet()) {
			Error error = new Error(entry.getKey(), entry.getValue());
			errorList.add(error);
		}

		return this;
	}

	@Override
	public Output addMessage(String key, String msg) {
		messageList.add(new Message(key, msg));
		return this;
	}

	@Override
	public boolean hasError() {
		return !errorList.isEmpty();
	}

	@Override
	public Error getError(String key) {
		if (key == null) {
			throw new IllegalArgumentException("key不能为空");
		}

		for (Error error : errorList) {
			if (key.equals(error.getKey())) {
				return error;
			}
		}

		return null;
	}

	@Override
	public List<Error> getErrorList() {
		return errorList;
	}

	static Output create() {
		return new OutputImpl();
	}

	@Override
	public int getErrorCount() {
		return errorList.size();
	}

	@Override
	public Output setParameter(String key, Object value) {
		if (key != null) {
			parameter.put(key, value);
		}

		return this;
	}

	@Override
	public Map<String, Object> getParameters() {
		return parameter;
	}

	@Override
	public Message getMessage(String key) {
		if (key == null) {
			throw new IllegalArgumentException("key不能为空");
		}

		for (Message message : messageList) {
			if (key.equals(message.getKey())) {
				return message;
			}
		}

		return null;
	}

	@Override
	public List<Message> getMessageList() {
		return messageList;
	}

	@Override
	public boolean hasMessage() {
		return !messageList.isEmpty();
	}

	@Override
	public Output setParameter(Map<String, Object> paramMap) {
		if (paramMap != null) {
			for (Entry<String, Object> entry : paramMap.entrySet()) {
				if (entry.getKey() != null) {
					parameter.put(entry.getKey(), entry.getValue());
				}
			}
		}

		return null;
	}

	@Override
	public boolean containsParameter(String key) {
		if (key == null) {
			return false;
		}

		return parameter.containsKey(key);
	}

	@Override
	public String getTrimString(String key) {
		String value = this.getParameter(key);
		return value != null ? value.trim() : value;
	}
}
